import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jinke/utils/component/component.dart';
import 'package:jinke/utils/util.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class CreateQuesPage extends GetView<CreateQuesController> {
  const CreateQuesPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    final state = controller.state;
    return ListView(
      padding: EdgeInsets.all(12.r),
      children: [
        CornerContainer(
          child: UITextField(
            controller: state.nameCtrl,
            hintText: '请输入具体问题',
            prefixIcon: const NeedText(text: '问题名称: '),
            prefixWidth: 90.r,
            border: const Border(),
            textAlign: TextAlign.right,
          ),
        ),
        SizedBox(
          height: 10.r,
        ),
        BrnTextSelectFormItem(
          title: '牵头人:',
          isRequire: true,
          hint: '请选择问题解决的牵头人员，单选',
          value: state.lead?.userName ?? '',
          onTap: () {
            controller.actionLead();
          },
        ),
        SizedBox(
          height: 10.r,
        ),
        BrnTextSelectFormItem(
          title: '协同人:',
          isRequire: true,
          hint: '请选择问题解决的协同人员，可多选',
          value: state.helps?.map((e) => e.userName ?? '').join(','),
          onTap: controller.actionHelps,
        ),
        Container(
          margin: EdgeInsets.only(top: 10.r),
          padding: EdgeInsets.fromLTRB(16.r, 0, 16.r, 16.r),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(4.r),
          ),
          child: Column(
            children: [
              Container(
                height: 50.r,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '解决措施：',
                      style:
                          TextStyle(color: ColorsUtil.title, fontSize: 16.sp),
                    ),
                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.all(gap15),
                decoration: BoxDecoration(
                  color: ColorsUtil.bg,
                  borderRadius: BorderRadius.circular(4.r),
                ),
                child: TextField(
                  controller: controller.state.resulveCtrl,
                  maxLength: 800,
                  maxLines: 4,
                  textAlignVertical: TextAlignVertical.top,
                  style: TextStyle(
                    color: ColorsUtil.title,
                    fontSize: 14.sp,
                  ),
                  decoration: InputDecoration(
                    fillColor: ColorsUtil.bg,
                    isCollapsed: false,
                    border: InputBorder.none,
                    hintText: '请输入组织或考核或激励或问责或保障等措施',
                    hintStyle: TextStyle(
                      color: ColorsUtil.placeholder,
                      fontSize: 14.sp,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        SizedBox(
          height: 10.r,
        ),
        BrnTextSelectFormItem(
          title: '完成时限:',
          isRequire: true,
          hint: '请选择问题的计划解决日期',
          value: state.time,
          onTap: controller.actionTime,
        ),
        SizedBox(
          height: 10.r,
        ),
        Container(
          margin: EdgeInsets.only(bottom: 50.r),
          decoration: const BoxDecoration(
            color: Colors.white,
          ),
          child: Column(
            children: [
              BrnTextSelectFormItem(
                title: '问题状态:',
                isRequire: true,
                value: state.quess
                    .firstWhere((element) => element.isSelected)
                    .name,
                onTap: controller.actionTime,
              ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 12.r),
                child: BrnSelectTag(
                  tags: state.quess.map((e) => e.name).toList(),
                  initTagState: [true],
                  onSelect: (p0) {
                    for (var i = 0; i < state.quess.length; i++) {
                      var item = state.quess[i];
                      item.isSelected = p0.contains(i);
                    }
                    state.status = p0.first;
                    controller.update();
                  },
                ),
              ),
              SizedBox(
                height: 10.r,
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget CornerContainer({
    required Widget child,
  }) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(4),
      ),
      padding: EdgeInsets.symmetric(horizontal: 12.r, vertical: 10.r),
      child: child,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CreateQuesController>(
      builder: (_) {
        return Scaffold(
          appBar: detailAppBar(context, title: '新建问题'),
          body: SafeArea(
            bottom: true,
            child: _buildView(),
          ),
          floatingActionButtonLocation:
              FloatingActionButtonLocation.centerDocked,
          floatingActionButton: InkWell(
              onTap: controller.actionSure,
              child: SafeArea(
                bottom: true,
                child: Container(
                  width: 1.sw,
                  height: 44.r,
                  alignment: Alignment.center,
                  color: ColorsUtil.blue,
                  child: Text(
                    '确认创建',
                    style: TextStyle(color: Colors.white, fontSize: 13.sp),
                  ),
                ),
              )),
        );
      },
    );
  }
}
